package com.atguigu.yygh.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.repository.DepartmentRepository;
import com.atguigu.yygh.service.DepartmentService;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zr
 * @date 2021/12/12 21:32
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;

    /**
     * 上传科室信息
     *
     * @param paramMap
     */
    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        // 将map转化为对象
        String resultMap = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(resultMap, Department.class);

        // 通过医院编号 和科室编号 判断
        Department departmentExists = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExists != null) {
            BeanUtils.copyProperties(department, departmentExists);
            departmentRepository.save(departmentExists);
        }
        department.setCreateTime(new Date());
        department.setUpdateTime(new Date());
        department.setIsDeleted(0);
        departmentRepository.save(department);
    }

    @Override
    public Page<Department> findPageDepartment(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher exampleMatcher = ExampleMatcher
                .matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.STARTING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);
        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    @Override
    public void removeDepartment(String hosCode, String depCode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hosCode) {
        // 用来封装最后的数据
        List<DepartmentVo> result = new ArrayList<>();
        // 根据医院编号 查询所有科室信息
        Department departmentQuery = new Department();
        departmentQuery.setHoscode(hosCode);
        Example<Department> example = Example.of(departmentQuery);
        // 所有科室列表
        List<Department> departmentList = departmentRepository.findAll(example);
        // 利用新特性 按照 大科室编号 进行分组 获取大科室下面的下级子科室
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // key  是对应的大科室编号  value  是对应的所有大科室数据
        for (Map.Entry<String, List<Department>> listEntry : departmentMap.entrySet()) {
            // 大科室对应的编号
            String bigCode = listEntry.getKey();
            // 大科室对应的全局数据
            List<Department> bigDepartmentList = listEntry.getValue();
            // 封装大科室数据
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            bigDepartmentVo.setDepcode(bigCode);
            bigDepartmentVo.setDepname(bigDepartmentList.get(0).getDepname());
            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            // 遍历全局数据
            for (Department department : bigDepartmentList) {
                DepartmentVo smallDepartmentVo = new DepartmentVo();
                smallDepartmentVo.setDepname(department.getDepname());
                smallDepartmentVo.setDepcode(department.getDepcode());
                // 将小科室中的数据 添加到集合中
                children.add(smallDepartmentVo);
            }
            // 把小科室list集合放到大科室children里面
            bigDepartmentVo.setChildren(children);
            result.add(bigDepartmentVo);
        }
        return result;
    }

    /**
     * 根据医院编号 和科室编号 获取科室名称
     *
     * @param hosCode
     * @param depCode
     * @return
     */
    @Override
    public String getDepName(String hosCode, String depCode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hosCode, String depCode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hosCode, depCode);
        if (department != null) {
            return department;
        }
        return null;
    }


}
